package com.wangwei.authoritySystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangwei.authoritySystem.common.utils.DepartmentTree;
import com.wangwei.authoritySystem.mapper.UserMapper;
import com.wangwei.authoritySystem.pojo.Department;
import com.wangwei.authoritySystem.service.DepartmentService;
import com.wangwei.authoritySystem.mapper.DepartmentMapper;
import com.wangwei.authoritySystem.vo.DepartmentQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
    implements DepartmentService{

    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    UserMapper userMapper;
    /**
     * 部门列表
     * @param departmentQueryVo
     * @return
     */
    @Override
    public List<Department> findDepartmentList(DepartmentQueryVo departmentQueryVo) {
        //先查询出部列表
        List<Department> departmentList = departmentMapper.selectByDepartmentName(departmentQueryVo.getDepartmentName());
        //生成部门树
        List<Department> departmentTree = DepartmentTree.createDepartmentTree(departmentList,0L);
        return departmentTree;
    }

    /**
     * 生成部门树
     * @return
     */
    @Override
    public List<Department> findParentDepartment() {
        //先查询出部列表
        QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
        queryWrapper.orderByAsc("order_num");
        List<Department> departmentList = baseMapper.selectList(queryWrapper);
/*        //创建部门对象
        Department department = new Department();
        //默认给个最顶级部门
        department.setId(0L);
        department.setDepartmentName("顶级部门");
        department.setPid(-1L);
        departmentList.add(department);*/
        //生成树
        List<Department> departmentTree = DepartmentTree.createDepartmentTree(departmentList,0L);
        return departmentTree;
    }

    /**
     * 检查是否有子部门
     * @param id
     * @return
     */
    @Override
    public int checkPid(Long id) {
        int count = departmentMapper.checkPid(id);
        return count;
    }

    @Override
    public int checkUser(Long id) {
        int count = userMapper.checkUser(id);
        return count;
    }
}




